


//
//  HHPNavigationController.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/4/17.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "HHPNavigationController.h"

@interface HHPNavigationController ()

@end

@implementation HHPNavigationController

+ (void)initialize
{
    UINavigationBar *bar = [UINavigationBar appearance];
    [bar setBackgroundImage:[UIImage imageNamed:@"navBar_bg_414x70"] forBarMetrics:UIBarMetricsDefault];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        
        UIButton *leftButton = [[UIButton alloc] init];
        [leftButton setImage:[UIImage imageNamed:@"back_9x16"] forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [leftButton sizeToFit];
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    }
    
    // 如果自定义返回按钮后, 滑动返回可能失效, 需要添加下面的代码
//    __weak typeof(viewController)Weakself = viewController;
//    self.interactivePopGestureRecognizer.delegate = (id)Weakself;
    
    if (viewController) {
        [super pushViewController:viewController animated:animated];
    }
}

- (void)back
{
    // 判断两种情况: push 和 present
    if ((self.presentedViewController || self.presentingViewController) && self.childViewControllers.count == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else {
        [self popViewControllerAnimated:YES];
    }
}

@end
